package com.activemq.springtest.service.impl;

import com.activemq.springtest.msg.TxMessage;
import com.activemq.springtest.service.JMSService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.TextMessage;


@Service
public class JMSServiceImpl implements JMSService {
    @Autowired
    private JmsTemplate jmsTemplate;
    @Autowired
    //@Resource(name = "destinationQueue")//Qualifier一样效果
    @Qualifier("destinationQueue")
    private Destination queue;

    /**
     * 发送消息
     *
     * @param msg
     */
    @Override
    public void sendMsg(String msg) {
        jmsTemplate.send(queue, new TxMessage(msg));
    }

    @Override
    public void recMsg() {
        TextMessage textMessage = (TextMessage) jmsTemplate.receive(queue);
        try {
            System.out.println(textMessage.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}
